package com.hly.versioncontroladmin.controller;


import com.hly.versioncontroladmin.domain.Bo.VcApplicationBo;
import com.hly.versioncontroladmin.domain.VcApplication;
import com.hly.versioncontroladmin.domain.VcVersion;
import com.hly.versioncontroladmin.result.Code;
import com.hly.versioncontroladmin.result.PageQuery;
import com.hly.versioncontroladmin.result.Result;
import com.hly.versioncontroladmin.service.VcApplicationService;
import com.hly.versioncontroladmin.service.VcVersionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api/app")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class VcApplicationController {

    private final VcApplicationService applicationService;
    private final VcVersionService versionService;

    @GetMapping("/{id}")
    public Result getById(@PathVariable("id") String id){
        return Result.ok(applicationService.getById(id));
    }

    @GetMapping("/list")
    public Result list(PageQuery<VcApplication> pageQuery, VcApplicationBo bo){
        return Result.ok(applicationService.listQuery(pageQuery, bo));
    }

    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody VcApplication application){
        return Result.ok(applicationService.saveOrUpdate(application));
    }

    @PostMapping("/del/{ids}")
    public Result del(@PathVariable("ids") String ids){
        List<String> strings = Arrays.asList(ids.split(","));
        if (versionService.lambdaQuery()
                .in(VcVersion::getAppId,
                        strings).list().size() > 0){
            Code.BUSINESS_ERR("该分类存在应用不可删除！请先删除应用");
        }
        return Result.ok(applicationService.removeBatchByIds(strings));
    }

}
